package br.gov.cemaden.library.model.previsao;

import br.gov.cemaden.library.model.alerta.Agencia;
import br.gov.cemaden.library.repository.previsao.AquisicaoPrecipitacaoRepository;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;

/**
 *
 * @author alan.oliveira
 */
@Entity
@Table(name = "previsao_precipitacao_aquisicao", schema = "previsao")
@SequenceGenerator(name = "sequence", sequenceName = "previsao.previsao_precipitacao_aquisicao_sequence", schema = "previsao")
public class AquisicaoPrecipitacao implements Serializable{
    
    @Id
    @GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
    @Column(name = "id_previsao_precipitacao_aquisicao")
    private Long id;        
    
    @ManyToOne(optional = false)
    @JoinColumn(name = "id_agencia", referencedColumnName = "id_agencia")
    private Agencia agencia;
    
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name = "datahora_arquivo")
    private Date datahoraArquivo;
    
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name = "datahora_aquisicao")
    private Date aquisicao;
    
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name = "datahora_atualizacao")
    private Date atualizacao;
    
    @OneToMany(mappedBy = "aquisicao", cascade = CascadeType.ALL)
    private List<PrevisaoPrecipitacao> previsoes;
    
    @Transient
    private AquisicaoPrecipitacaoRepository repository;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }    

    public Agencia getAgencia() {
        return agencia;
    }

    public void setAgencia(Agencia agencia) {
        this.agencia = agencia;
    }

    public Date getDatahoraArquivo() {
        return datahoraArquivo;
    }

    public void setDatahoraArquivo(Date datahoraArquivo) {
        this.datahoraArquivo = datahoraArquivo;
    }

    public Date getAquisicao() {
        return aquisicao;
    }

    public void setAquisicao(Date aquisicao) {
        this.aquisicao = aquisicao;
    }

    public Date getAtualizacao() {
        return atualizacao;
    }

    public void setAtualizacao(Date atualizacao) {
        this.atualizacao = atualizacao;
    }        
    
    public List<PrevisaoPrecipitacao> getPrevisoes() {
        return previsoes;
    }

    public void setPrevisoes(List<PrevisaoPrecipitacao> previsoes) {
        this.previsoes = previsoes;
    }

    public AquisicaoPrecipitacaoRepository getRepository(EntityManager em) {
        if(repository == null)
            this.setRepository(new AquisicaoPrecipitacaoRepository(em));
        return repository;
    }

    public void setRepository(AquisicaoPrecipitacaoRepository repository) {
        this.repository = repository;
    }

    @Override
    public String toString() {
        return String.format("%s %s", this.aquisicao.toString(), (this.agencia != null) ? this.agencia.getNome() : "");
    } 

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 73 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 73 * hash + (this.datahoraArquivo != null ? this.datahoraArquivo.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AquisicaoPrecipitacao other = (AquisicaoPrecipitacao) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
    
    
    
    public void save(EntityManager em) throws NullPointerException{
        if(em.isOpen())
            getRepository(em).persist(this);
        else
            throw new NullPointerException("The database connection is closed.");
    }
        
    public AquisicaoPrecipitacao update(EntityManager em) {
        if(em.isOpen())
            return getRepository(em).update(this);
        else
            throw new NullPointerException("The database connection is closed");
    }
    
    public Boolean exists(EntityManager em){
        if(em.isOpen()){
            return getRepository(em).exists(this);            
        }
        else {
            throw new NullPointerException("The database connection is closed.");
        }
                
    }
    
}
